package com.yd.controller.yd;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.yd.pojo.Customer;
import com.yd.pojo.Supplier;
import com.yd.pojo.Administrator;
import com.yd.pojo.Agency;
import com.yd.service.CustomerService;
import com.yd.service.SupplierService;
import com.yd.service.AdministratorService;
import com.yd.service.AgencyService;
import com.yd.tool.UtsReg;


@Controller
public class UsersControllerYD {

	@Resource
	private AgencyService agencyService;
	@Resource
	private SupplierService supplierService;
	@Resource
	private CustomerService customerService;
	@Resource
	private AdministratorService administratorService;

	@RequestMapping(value={"/app/supplier/token/getuser"})
	@ResponseBody
	public String app_supplier_token(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String key = "";
		String token = "";
		if (UtsReg.getStringKB(request.getParameter("key"))){key = request.getParameter("key").trim();} else {jsonObject.put("msg","key错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();} else {jsonObject.put("msg","token错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		if(!key.equals("yydd")){
			jsonObject.put("msg","key不正确");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		Supplier supplier=supplierService.selectOne(new EntityWrapper<Supplier>().eq("token", token));
		if(supplier==null){
			jsonObject.put("msg","token不正确");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		supplier.setLoginpass("");

		jsonObject.put("data",supplier);
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();
	}
	
	@RequestMapping(value={"/app/agency/token/getuser"})
	@ResponseBody
	public String app_agency_token_getuser(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String key = "";
		String token = "";
		if (UtsReg.getStringKB(request.getParameter("key"))){key = request.getParameter("key").trim();} else {jsonObject.put("msg","key错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();} else {jsonObject.put("msg","token错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		if(!key.equals("yydd")){
			jsonObject.put("msg","key不正确");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		Agency agency=agencyService.selectOne(new EntityWrapper<Agency>().eq("token", token));
		if(agency==null){
			jsonObject.put("msg","user不存在");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		jsonObject.put("data",agency);
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();
	}
	
	@RequestMapping(value={"/app/customer/token/getuser"})
	@ResponseBody
	public String app_customer_token_getuser(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String key = "";
		String token = "";
		if (UtsReg.getStringKB(request.getParameter("key"))){key = request.getParameter("key").trim();} else {jsonObject.put("msg","key错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();} else {jsonObject.put("msg","token错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		if(!key.equals("yydd")){
			jsonObject.put("msg","key不正确");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		Customer customer=customerService.selectOne(new EntityWrapper<Customer>().eq("token", token));
		if(customer==null){
			jsonObject.put("msg","user不存在");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		jsonObject.put("data",customer);
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();
	}
	
	@RequestMapping(value={"/app/administrator/token/getuser"})
	@ResponseBody
	public String app_administrator_token_getuser(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String key = "";
		String token = "";
		if (UtsReg.getStringKB(request.getParameter("key"))){key = request.getParameter("key").trim();} else {jsonObject.put("msg","key错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();} else {jsonObject.put("msg","token错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		if(!key.equals("yydd")){
			jsonObject.put("msg","key不正确");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		Administrator administrator=administratorService.selectOne(new EntityWrapper<Administrator>().eq("token", token));
		if(administrator==null){
			jsonObject.put("msg","sys不存在");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		jsonObject.put("data",administrator);
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();
	}
	
	@RequestMapping(value={"/app/agency/id/getuser"})
	@ResponseBody
	public String app_agency_id_getuser(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String key = "";
		String id = "";
		if (UtsReg.getStringKB(request.getParameter("key"))){key = request.getParameter("key").trim();} else {jsonObject.put("msg","key错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("id"))){id = request.getParameter("id").trim();} else {jsonObject.put("msg","id错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		if(!key.equals("yydd")){
			jsonObject.put("msg","key不正确");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		Agency agency=agencyService.selectOne(new EntityWrapper<Agency>().eq("id", id));
		if(agency==null){
			jsonObject.put("msg","此id不存在");jsonObject.put("error", "1");return jsonObject.toString();
		}
		agency.setLoginpass("");

		jsonObject.put("data",agency);
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();
	}
	
	@RequestMapping(value={"/app/supplier/id/getuser"})
	@ResponseBody
	public String app_supplier_id_getuser(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String key = "";
		String id = "";
		if (UtsReg.getStringKB(request.getParameter("key"))){key = request.getParameter("key").trim();} else {jsonObject.put("msg","key错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("id"))){id = request.getParameter("id").trim();} else {jsonObject.put("msg","id错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		if(!key.equals("yydd")){
			jsonObject.put("msg","key不正确");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		Supplier supplier=supplierService.selectOne(new EntityWrapper<Supplier>().eq("id", id));
		if(supplier==null){
			jsonObject.put("msg","user不存在");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		jsonObject.put("data",supplier);
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();
	}
	
	@RequestMapping(value={"/app/customer/id/getuser"})
	@ResponseBody
	public String app_customer_id_getuser(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String key = "";
		String id = "";
		if (UtsReg.getStringKB(request.getParameter("key"))){key = request.getParameter("key").trim();} else {jsonObject.put("msg","key错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("id"))){id = request.getParameter("id").trim();} else {jsonObject.put("msg","id错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		if(!key.equals("yydd")){
			jsonObject.put("msg","key不正确");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		Customer customer=customerService.selectOne(new EntityWrapper<Customer>().eq("id", id));
		if(customer==null){
			jsonObject.put("msg","user不存在");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		jsonObject.put("data",customer);
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();
	}
	
}



